{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 1: Extracting Correlation Coefficients\n",
    "\n",
    "Here, we look at the data containing log returns for the S&P 500 companies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "import re"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Data is located at: `../data/2_Stock_Prices.csv`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>MSFT</th>\n",
       "      <th>AMZN</th>\n",
       "      <th>BRK-B</th>\n",
       "      <th>FB</th>\n",
       "      <th>JPM</th>\n",
       "      <th>JNJ</th>\n",
       "      <th>GOOG</th>\n",
       "      <th>XOM</th>\n",
       "      <th>BAC</th>\n",
       "      <th>...</th>\n",
       "      <th>APH</th>\n",
       "      <th>YUM</th>\n",
       "      <th>ORLY</th>\n",
       "      <th>GIS</th>\n",
       "      <th>DG</th>\n",
       "      <th>GLW</th>\n",
       "      <th>ADM</th>\n",
       "      <th>PPG</th>\n",
       "      <th>PXD</th>\n",
       "      <th>DFS</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-02</th>\n",
       "      <td>102.3183</td>\n",
       "      <td>42.7921</td>\n",
       "      <td>308.52</td>\n",
       "      <td>149.17</td>\n",
       "      <td>78.450</td>\n",
       "      <td>57.2584</td>\n",
       "      <td>94.2874</td>\n",
       "      <td>524.81</td>\n",
       "      <td>80.9533</td>\n",
       "      <td>16.9308</td>\n",
       "      <td>...</td>\n",
       "      <td>51.9209</td>\n",
       "      <td>47.9199</td>\n",
       "      <td>192.11</td>\n",
       "      <td>46.8581</td>\n",
       "      <td>67.7405</td>\n",
       "      <td>21.0478</td>\n",
       "      <td>46.6881</td>\n",
       "      <td>108.3730</td>\n",
       "      <td>149.5115</td>\n",
       "      <td>60.6054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>99.4358</td>\n",
       "      <td>42.3940</td>\n",
       "      <td>302.19</td>\n",
       "      <td>147.00</td>\n",
       "      <td>77.190</td>\n",
       "      <td>55.4808</td>\n",
       "      <td>93.6289</td>\n",
       "      <td>513.87</td>\n",
       "      <td>78.7383</td>\n",
       "      <td>16.4389</td>\n",
       "      <td>...</td>\n",
       "      <td>50.9724</td>\n",
       "      <td>46.9463</td>\n",
       "      <td>188.72</td>\n",
       "      <td>46.0111</td>\n",
       "      <td>67.3003</td>\n",
       "      <td>20.7551</td>\n",
       "      <td>45.0726</td>\n",
       "      <td>105.5763</td>\n",
       "      <td>139.4769</td>\n",
       "      <td>59.2516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>99.4452</td>\n",
       "      <td>41.7763</td>\n",
       "      <td>295.29</td>\n",
       "      <td>146.84</td>\n",
       "      <td>76.150</td>\n",
       "      <td>54.0423</td>\n",
       "      <td>93.1688</td>\n",
       "      <td>501.96</td>\n",
       "      <td>78.3197</td>\n",
       "      <td>15.9471</td>\n",
       "      <td>...</td>\n",
       "      <td>50.3238</td>\n",
       "      <td>46.3701</td>\n",
       "      <td>188.08</td>\n",
       "      <td>45.8964</td>\n",
       "      <td>66.4485</td>\n",
       "      <td>20.3984</td>\n",
       "      <td>44.1841</td>\n",
       "      <td>105.3164</td>\n",
       "      <td>139.1079</td>\n",
       "      <td>57.8607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>100.8396</td>\n",
       "      <td>42.3071</td>\n",
       "      <td>298.42</td>\n",
       "      <td>148.88</td>\n",
       "      <td>76.150</td>\n",
       "      <td>54.1247</td>\n",
       "      <td>95.2256</td>\n",
       "      <td>501.10</td>\n",
       "      <td>79.1133</td>\n",
       "      <td>16.0228</td>\n",
       "      <td>...</td>\n",
       "      <td>51.1853</td>\n",
       "      <td>47.9067</td>\n",
       "      <td>189.79</td>\n",
       "      <td>46.8669</td>\n",
       "      <td>67.2524</td>\n",
       "      <td>20.6911</td>\n",
       "      <td>44.8482</td>\n",
       "      <td>105.7841</td>\n",
       "      <td>137.5817</td>\n",
       "      <td>58.7230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>104.7141</td>\n",
       "      <td>43.5517</td>\n",
       "      <td>300.46</td>\n",
       "      <td>151.37</td>\n",
       "      <td>78.175</td>\n",
       "      <td>55.3342</td>\n",
       "      <td>95.9743</td>\n",
       "      <td>502.68</td>\n",
       "      <td>80.4301</td>\n",
       "      <td>16.3538</td>\n",
       "      <td>...</td>\n",
       "      <td>52.1823</td>\n",
       "      <td>48.7412</td>\n",
       "      <td>193.24</td>\n",
       "      <td>47.6256</td>\n",
       "      <td>66.5250</td>\n",
       "      <td>21.3588</td>\n",
       "      <td>44.1930</td>\n",
       "      <td>108.5998</td>\n",
       "      <td>141.0529</td>\n",
       "      <td>59.6688</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 199 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                AAPL     MSFT    AMZN   BRK-B      FB      JPM      JNJ  \\\n",
       "Date                                                                      \n",
       "2015-01-02  102.3183  42.7921  308.52  149.17  78.450  57.2584  94.2874   \n",
       "2015-01-05   99.4358  42.3940  302.19  147.00  77.190  55.4808  93.6289   \n",
       "2015-01-06   99.4452  41.7763  295.29  146.84  76.150  54.0423  93.1688   \n",
       "2015-01-07  100.8396  42.3071  298.42  148.88  76.150  54.1247  95.2256   \n",
       "2015-01-08  104.7141  43.5517  300.46  151.37  78.175  55.3342  95.9743   \n",
       "\n",
       "              GOOG      XOM      BAC  ...      APH      YUM    ORLY      GIS  \\\n",
       "Date                                  ...                                      \n",
       "2015-01-02  524.81  80.9533  16.9308  ...  51.9209  47.9199  192.11  46.8581   \n",
       "2015-01-05  513.87  78.7383  16.4389  ...  50.9724  46.9463  188.72  46.0111   \n",
       "2015-01-06  501.96  78.3197  15.9471  ...  50.3238  46.3701  188.08  45.8964   \n",
       "2015-01-07  501.10  79.1133  16.0228  ...  51.1853  47.9067  189.79  46.8669   \n",
       "2015-01-08  502.68  80.4301  16.3538  ...  52.1823  48.7412  193.24  47.6256   \n",
       "\n",
       "                 DG      GLW      ADM       PPG       PXD      DFS  \n",
       "Date                                                                \n",
       "2015-01-02  67.7405  21.0478  46.6881  108.3730  149.5115  60.6054  \n",
       "2015-01-05  67.3003  20.7551  45.0726  105.5763  139.4769  59.2516  \n",
       "2015-01-06  66.4485  20.3984  44.1841  105.3164  139.1079  57.8607  \n",
       "2015-01-07  67.2524  20.6911  44.8482  105.7841  137.5817  58.7230  \n",
       "2015-01-08  66.5250  21.3588  44.1930  108.5998  141.0529  59.6688  \n",
       "\n",
       "[5 rows x 199 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load the data\n",
    "df_all = pd.read_csv('../data/2_Stock_Prices.csv', index_col=0)\n",
    "\n",
    "# Pick 2015 data\n",
    "df = df_all.loc[\"2015-01-02\":\"2016-01-02\"]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>MSFT</th>\n",
       "      <th>AMZN</th>\n",
       "      <th>BRK-B</th>\n",
       "      <th>FB</th>\n",
       "      <th>JPM</th>\n",
       "      <th>JNJ</th>\n",
       "      <th>GOOG</th>\n",
       "      <th>XOM</th>\n",
       "      <th>BAC</th>\n",
       "      <th>...</th>\n",
       "      <th>APH</th>\n",
       "      <th>YUM</th>\n",
       "      <th>ORLY</th>\n",
       "      <th>GIS</th>\n",
       "      <th>DG</th>\n",
       "      <th>GLW</th>\n",
       "      <th>ADM</th>\n",
       "      <th>PPG</th>\n",
       "      <th>PXD</th>\n",
       "      <th>DFS</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>-0.028576</td>\n",
       "      <td>-0.009347</td>\n",
       "      <td>-0.020731</td>\n",
       "      <td>-0.014654</td>\n",
       "      <td>-0.016192</td>\n",
       "      <td>-0.031537</td>\n",
       "      <td>-0.007008</td>\n",
       "      <td>-0.021066</td>\n",
       "      <td>-0.027743</td>\n",
       "      <td>-0.029484</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.018437</td>\n",
       "      <td>-0.020526</td>\n",
       "      <td>-0.017804</td>\n",
       "      <td>-0.018241</td>\n",
       "      <td>-0.006520</td>\n",
       "      <td>-0.014004</td>\n",
       "      <td>-0.035215</td>\n",
       "      <td>-0.026145</td>\n",
       "      <td>-0.069474</td>\n",
       "      <td>-0.022591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>0.000095</td>\n",
       "      <td>-0.014678</td>\n",
       "      <td>-0.023098</td>\n",
       "      <td>-0.001089</td>\n",
       "      <td>-0.013565</td>\n",
       "      <td>-0.026270</td>\n",
       "      <td>-0.004926</td>\n",
       "      <td>-0.023450</td>\n",
       "      <td>-0.005331</td>\n",
       "      <td>-0.030373</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.012806</td>\n",
       "      <td>-0.012350</td>\n",
       "      <td>-0.003397</td>\n",
       "      <td>-0.002496</td>\n",
       "      <td>-0.012737</td>\n",
       "      <td>-0.017336</td>\n",
       "      <td>-0.019910</td>\n",
       "      <td>-0.002465</td>\n",
       "      <td>-0.002649</td>\n",
       "      <td>-0.023754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>0.013924</td>\n",
       "      <td>0.012626</td>\n",
       "      <td>0.010544</td>\n",
       "      <td>0.013797</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.001524</td>\n",
       "      <td>0.021836</td>\n",
       "      <td>-0.001715</td>\n",
       "      <td>0.010082</td>\n",
       "      <td>0.004736</td>\n",
       "      <td>...</td>\n",
       "      <td>0.016974</td>\n",
       "      <td>0.032601</td>\n",
       "      <td>0.009051</td>\n",
       "      <td>0.020925</td>\n",
       "      <td>0.012025</td>\n",
       "      <td>0.014247</td>\n",
       "      <td>0.014918</td>\n",
       "      <td>0.004431</td>\n",
       "      <td>-0.011032</td>\n",
       "      <td>0.014793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>0.037703</td>\n",
       "      <td>0.028994</td>\n",
       "      <td>0.006813</td>\n",
       "      <td>0.016587</td>\n",
       "      <td>0.026245</td>\n",
       "      <td>0.022101</td>\n",
       "      <td>0.007832</td>\n",
       "      <td>0.003148</td>\n",
       "      <td>0.016507</td>\n",
       "      <td>0.020448</td>\n",
       "      <td>...</td>\n",
       "      <td>0.019291</td>\n",
       "      <td>0.017269</td>\n",
       "      <td>0.018015</td>\n",
       "      <td>0.016059</td>\n",
       "      <td>-0.010875</td>\n",
       "      <td>0.031760</td>\n",
       "      <td>-0.014717</td>\n",
       "      <td>0.026269</td>\n",
       "      <td>0.024917</td>\n",
       "      <td>0.015978</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>0.001072</td>\n",
       "      <td>-0.008442</td>\n",
       "      <td>-0.011818</td>\n",
       "      <td>-0.012631</td>\n",
       "      <td>-0.005580</td>\n",
       "      <td>-0.017540</td>\n",
       "      <td>-0.013722</td>\n",
       "      <td>-0.013035</td>\n",
       "      <td>-0.001411</td>\n",
       "      <td>-0.018091</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005768</td>\n",
       "      <td>-0.001768</td>\n",
       "      <td>-0.028502</td>\n",
       "      <td>-0.010130</td>\n",
       "      <td>-0.020200</td>\n",
       "      <td>-0.001710</td>\n",
       "      <td>-0.008565</td>\n",
       "      <td>-0.012740</td>\n",
       "      <td>0.007819</td>\n",
       "      <td>-0.016134</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 196 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                AAPL      MSFT      AMZN     BRK-B        FB       JPM  \\\n",
       "Date                                                                     \n",
       "2015-01-05 -0.028576 -0.009347 -0.020731 -0.014654 -0.016192 -0.031537   \n",
       "2015-01-06  0.000095 -0.014678 -0.023098 -0.001089 -0.013565 -0.026270   \n",
       "2015-01-07  0.013924  0.012626  0.010544  0.013797  0.000000  0.001524   \n",
       "2015-01-08  0.037703  0.028994  0.006813  0.016587  0.026245  0.022101   \n",
       "2015-01-09  0.001072 -0.008442 -0.011818 -0.012631 -0.005580 -0.017540   \n",
       "\n",
       "                 JNJ      GOOG       XOM       BAC  ...       APH       YUM  \\\n",
       "Date                                                ...                       \n",
       "2015-01-05 -0.007008 -0.021066 -0.027743 -0.029484  ... -0.018437 -0.020526   \n",
       "2015-01-06 -0.004926 -0.023450 -0.005331 -0.030373  ... -0.012806 -0.012350   \n",
       "2015-01-07  0.021836 -0.001715  0.010082  0.004736  ...  0.016974  0.032601   \n",
       "2015-01-08  0.007832  0.003148  0.016507  0.020448  ...  0.019291  0.017269   \n",
       "2015-01-09 -0.013722 -0.013035 -0.001411 -0.018091  ... -0.005768 -0.001768   \n",
       "\n",
       "                ORLY       GIS        DG       GLW       ADM       PPG  \\\n",
       "Date                                                                     \n",
       "2015-01-05 -0.017804 -0.018241 -0.006520 -0.014004 -0.035215 -0.026145   \n",
       "2015-01-06 -0.003397 -0.002496 -0.012737 -0.017336 -0.019910 -0.002465   \n",
       "2015-01-07  0.009051  0.020925  0.012025  0.014247  0.014918  0.004431   \n",
       "2015-01-08  0.018015  0.016059 -0.010875  0.031760 -0.014717  0.026269   \n",
       "2015-01-09 -0.028502 -0.010130 -0.020200 -0.001710 -0.008565 -0.012740   \n",
       "\n",
       "                 PXD       DFS  \n",
       "Date                            \n",
       "2015-01-05 -0.069474 -0.022591  \n",
       "2015-01-06 -0.002649 -0.023754  \n",
       "2015-01-07 -0.011032  0.014793  \n",
       "2015-01-08  0.024917  0.015978  \n",
       "2015-01-09  0.007819 -0.016134  \n",
       "\n",
       "[5 rows x 196 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Clean the data: Remove NaN values, get log returns\n",
    "df = df.dropna(axis=1)\n",
    "df = df.apply(np.log)\n",
    "# Get daily log-returns\n",
    "returns_df = df.diff().dropna(axis=0)\n",
    "returns_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plotting Median Correlation Coefficients\n",
    "\n",
    "Here, we find the median correlation coefficient for each company for the year 2015, and plot the results as a histogram."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEWCAYAAABhffzLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAYT0lEQVR4nO3deZRlZX3u8e8jDYIyQ0sIiAUGEpElatoZvUTUoNyAAxoQFRIid+l1xKhEXVfUxOBw1auJUQQFWQbBITIZwYvgGOahZVTE1osSQGQUo4K/+8feJcfqqu7T3bWruvr9ftY6q/e8f+851U/tes/Z70lVIUlqxwPmuwBJ0twy+CWpMQa/JDXG4Jekxhj8ktQYg1+SGmPwS1JjDH6tdZLskeQ7Se5I8vMk307yuJH1Oyb5epK7kixL8rJpjlFJfpHk7iQ/SfKBJOvNcL5lSX7Zb3t3krOmrH99kv/s6/lkkgeOrJt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JLD+3Xf4f7B9w4CvjXmMc8EXpFk/f6YuyR58DR139iH8EvpBhADuItuOOjpjD4fewI/m7yKXpmq+gFdl9M7Rp7nnfs+9W8Az03yoL7O5wHfBM4G9k/ykH77LbNq37N8DfCHSR7X779JujfPR8302q/IbyafW60dDH7NqKpuqKr/M82qd9F1fSxN93HHd/XL/wXYuO/ieRNwwTT7fgVY1G/zLrqhk1elpqXA64ATk1xN9x7B5FfQvQb4q/7YLwVeO+ZhjwGuAi7p2/Nxlr9a/ShwcJLz6Lp5Jv9iWArc278Z+vop+xwJLOnrOYr7hwUf198AfwBcl+S7wCeAn1bVJXT98hcA5wPHVNWlVXUV8DbgrP6cX+X+52alqvuqw78EPpLk8n7/DadsNtNrvyJH99v75u5awtE5JakxXvFLUmMMfklqjMEvSY0x+CWpMQa/JDXG4Jekxhj8ktSY/w/dtrum2jbJhgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Find the median correlation for each company for 2015 \n",
    "median_corr_arr = np.zeros(len(returns_df.columns))\n",
    "corr_matrix = returns_df.corr()\n",
    "for idx, company in enumerate(returns_df.columns):\n",
    "    # Don't take the 1.0 correlation with the company itself \n",
    "    mask = corr_matrix.index != company\n",
    "    corr_list = corr_matrix[f'{company}'][mask]\n",
    "    median_corr_arr[idx] = np.median(corr_list)\n",
    "\n",
    "# Plot the results\n",
    "fig, ax = plt.subplots(1,1)\n",
    "bins = np.linspace(0,1,21)\n",
    "ax.hist(median_corr_arr,bins=bins)\n",
    "ax.set_xlabel('Median Correlation Coefficient')\n",
    "ax.set_ylabel('Counts')\n",
    "ax.set_title('S&P 500: 2015')\n",
    "fig.savefig('./output/median_corr_coeff_dist.pdf')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Identifying Companies with Min/Max Correlation Coeff.\n",
    "\n",
    "Here, we identify the five companies with minimum and maximum correlation coefficients for 2015."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************\n",
      "Top 5 Companies With Max Corr. Coef.:\n",
      "****************************************\n",
      "BRK-B: 0.607\n",
      "MMC: 0.599\n",
      "FISV: 0.574\n",
      "HON: 0.566\n",
      "ITW: 0.563\n",
      "****************************************\n",
      "Top 5 Companies With Min Corr. Coef.:\n",
      "****************************************\n",
      "HUM: 0.203\n",
      "KMI: 0.235\n",
      "CI: 0.268\n",
      "ILMN: 0.276\n",
      "NFLX: 0.278\n"
     ]
    }
   ],
   "source": [
    "# Map the median correlation values with the companies\n",
    "corr_map = {}\n",
    "for idx, company in enumerate(corr_matrix.columns):\n",
    "    corr_map[company] = median_corr_arr[idx]\n",
    "\n",
    "# Sort the coefficients\n",
    "sorted_corr_map = sorted(corr_map.items(), key=lambda item: item[1])\n",
    "\n",
    "# Minimum & Maximum 5:\n",
    "min5 = sorted_corr_map[:5]\n",
    "max5 = sorted_corr_map[-5:]\n",
    "max5.reverse()\n",
    "\n",
    "# Print results\n",
    "print('*'*40)\n",
    "print('Top 5 Companies With Max Corr. Coef.:')\n",
    "print('*'*40)\n",
    "for comp, corr in max5:\n",
    "    print(f'{comp}: {corr:.3f}')\n",
    "print('*'*40)\n",
    "print('Top 5 Companies With Min Corr. Coef.:')\n",
    "print('*'*40)\n",
    "for comp, corr in min5:\n",
    "    print(f'{comp}: {corr:.3f}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AAPL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MSFT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AMZN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BRK-B\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "FB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "JPM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "JNJ\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GOOG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "XOM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BAC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "UNH\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "V\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "WFC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PFE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "HD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "T\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "VZ\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CSCO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CVX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "INTC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MRK\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "C\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "KO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CMCSA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "NVDA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DIS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PEP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "NFLX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ABBV\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ORCL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "WMT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AMGN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MDT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MCD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ADBE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MMM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "IBM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "HON\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "UNP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ABT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ACN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "TXN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CRM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "COST\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "NKE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "LLY\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BMY\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "QCOM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "UTX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "TMO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GILD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AVGO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BKNG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "LOW\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "USB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "UPS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CAT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SLB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "COP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "LMT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "NEE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CVS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AXP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SBUX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BIIB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ANTM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "TJX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BDX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PNC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AET\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EOG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AMT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ADP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AGN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CSX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DHR\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CELG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MDLZ\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ISRG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "FDX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SCHW\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CME\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "OXY\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "WBA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BLK\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DUK\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "RTN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CHTR\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SPG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ATVI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SYK\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "INTU\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BK\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "NOC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SPGI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MU\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ESRX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ILMN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "NSC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "VLO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BSX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AIG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PSX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "COF\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EMR\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "FOXA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "TGT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CCI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "D\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MET\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "HUM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "VRTX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ICE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CTSH\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MMC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ZTS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ITW\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EXC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PLD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PRU\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "HPQ\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BBT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "KMB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AMAT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PGR\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ECL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MPC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SHW\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MAR\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "BAX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "HCA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DAL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "WM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ETN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AON\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ROST\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "F\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AFL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "APD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SYY\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "AEP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "FIS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "STZ\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "JCI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "TRV\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ALL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "LYB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ADI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "STI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EQIX\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "WMB\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "KMI\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "LUV\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "APC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "FISV\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ADSK\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "HAL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "TEL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "STT\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EBAY\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ROP\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PSA\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EL\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "SRE\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "REGN\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "CNC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "EW\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "MCO\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "VFC\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "APH\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "YUM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ORLY\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GIS\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "GLW\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "ADM\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PPG\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "PXD\n",
      "Index(['HUM', 'CI'], dtype='object')\n",
      "DFS\n",
      "Index(['HUM', 'CI'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# Track low correlation \n",
    "def low_corr(corr_matrix,company,threshold=0.2):\n",
    "    corr = corr_matrix[company]\n",
    "    low_corr = corr[corr<threshold]\n",
    "    num_low_corr = low_corr.size\n",
    "    comp_low_corr = low_corr.index\n",
    "    return comp_low_corr, num_low_corr\n",
    "    \n",
    "for comp in corr_matrix.columns:\n",
    "    comp_low_corr, num_low_corr = low_corr(corr_matrix, company)\n",
    "    if num_low_corr != 0:\n",
    "        print(comp)\n",
    "        print(f'{comp_low_corr}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 2: Finding Eigenvalues of the Correlation Matrix\n",
    "\n",
    "Here, we calculate the eigenvalue spectrum of the correlation matrix, looking at the whole S&P 500 data with all years included. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Take all data\n",
    "df = df_all\n",
    "# Clean the data: Remove NaN values, get log returns\n",
    "df = df.dropna(axis=1)\n",
    "df = df.apply(np.log)\n",
    "# Get daily log-returns\n",
    "returns_df = df.diff().dropna(axis=0)\n",
    "corr_matrix = returns_df.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtQAAAFPCAYAAABtZFntAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3dfbgkZX3m8e8tIBpFgTAQBIaRhBjF6KgTfEtcImpQXEFXDaxRXIlolF2JbhI0udTEvBBX1BgTDAQEN4ggiCJgFAnKuisoICCICpghjowMAvISDcngb/+oOtKcOS99Tp3u6jPz/VxXX939dHXX3dWnn/qdqqerUlVIkiRJWpwH9R1AkiRJWs4sqCVJkqQOLKglSZKkDiyoJUmSpA4sqCVJkqQOLKglSZKkDiyot0BJXpHkc33nmEuSLyT57b5zjEMaH05yR5KvDDH9yUn+tL29X5J1o08paRzsnzWsdl3w70nWjuj1b2xf/x9G8fqbGwvqzVSStUl+nOSegcsHAarq1Kp6Xt8Z9VO/CjwX2L2q9u07jKTRsn/WEnp3Va2aupNk2yQnJbkryfeTvHmYF0nyT0kqydZTbVX188CfL33kzdPW80+iZew/V9Xn+w6h+yXZuqo2TmveE1hbVf/aw7wl9cP+ecIM00cug370ncDeNOuVnwMuSvKNqvrH2Z6Q5BVYD3bmFuotUJJXJ/nSwP3nJflWkjuT/G2SLw7uzkvymiTXtUMSPptkz4HHKsnrk1zfPv437RCGbZP8MMnjB6Zd0W6V2TnJDknOTXJr+7xzk+w+S953Du5ySrJq8D/pJI9McmKS9Um+l+RPk2zVPvYL7fu5M8kPkpw+x3J5UZJr29xfSPLY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/tNJEmajX22JoEFtbSpPwI+UlVrsXOWpElnn63eWVBLA5I8Bngu8P62yc5ZkiaUfbYmRaqq7wySJEnSsuUWakmSJKkDC2pJkiSpAwtqSZIkqQMLakmSJKkDC2pJkiSpAwtqSZIkqQMLakmSJKkDC2pJkiSpg/8PjRB2BvWuYHkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 864x360 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Find the eigenvalues of the correlation matrix\n",
    "eigvals = np.linalg.eigvals(corr_matrix)\n",
    "bins_full = np.linspace(0,70)\n",
    "bins = np.linspace(0,4)\n",
    "\n",
    "# Plot the eigenvalue spectrum\n",
    "fig, ax = plt.subplots(1,2,figsize=(12,5))\n",
    "ax[0].hist(eigvals, bins=bins_full) # Entire range\n",
    "ax[0].set_xlabel(r'$\\lambda$')\n",
    "ax[0].set_ylabel('Counts')\n",
    "ax[0].set_title('Eigenvalues over full range')\n",
    "\n",
    "ax[1].hist(eigvals, bins=bins) # Within the range [0,4]\n",
    "ax[1].set_xlabel(r'$\\lambda$')\n",
    "ax[1].set_ylabel('Counts')\n",
    "ax[1].set_title('Eigenvalues over range [0,4]')\n",
    "fig.savefig('../output/eigval_dist_corrmatrix.pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Biggest eigenvalue of correlation matrix: 66.72\n"
     ]
    }
   ],
   "source": [
    "# Biggest eigenvalue\n",
    "print(f'Biggest eigenvalue of correlation matrix: {eigvals[0]:.2f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Unfolding\n",
    "\n",
    "Here, we calculate the unfolded eigenvalues and plot the new eigenvalue spectrum. We normalize the eigenvalues as follows:\n",
    "\n",
    "$\\xi_n = \\frac{\\lambda_n}{D(\\lambda_n)}$ where $\\lambda_n$ is the $n^{th}$ eigenvalue, and $D(\\lambda_n)$ is the mean eigenvalue spacing around energy level $n$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAATb0lEQVR4nO3dfbBddX3v8feHABemqEA5MimYHrDUqlSDPXJRnI5G2sGiV3SorddSdLjGa/UK1dob7b2tdtopzgg6Vmob5clKaalifcCHSxGwjA6Y0PDU6GBpbFMzJFxrheqkJnz7x16px+Q87JyctffJ+b1fM2fOWr/19D17cj5n5bd/+7dSVUiS2nHIuAuQJI2WwS9JjTH4JakxBr8kNcbgl6TGHDruAoZx3HHH1eTk5LjLkKSDysaNGx+uqom92w+K4J+cnGTDhg3jLkOSDipJvjFTu109ktQYg1+SGmPwS1JjDH5JaozBL0mNMfglqTG9BX+SI5LcmeTuJPcneWfXfnWSf0iyqfta3VcNkqR99TmOfyewpqoeTXIYcHuSz3bb3lpVH+3x2pKkWfQW/DWY6P/RbvWw7svJ/yVpzHr95G6SFcBG4CeAy6vqjiSvB34/yW8DNwPrqmrnDMeuBdYCrFq1qs8yl6TJdTcu+Ngtl5yziJVIWm56fXO3qnZX1WrgROD0JKcCbwN+Cng2cCzwv2c5dn1VTVXV1MTEPlNNSJIWaCSjeqrq28CtwNlVta0GdgJXAaePogZJ0kCfo3omkhzdLR8JnAV8NcnKri3AucB9fdUgSdpXn338K4Frun7+Q4Drq+rTSb6QZAIIsAn4nz3WIEnaS5+jeu4BTpuhfU1f15Qkzc9P7kpSYwx+SWqMwS9JjTH4JakxBr8kNcbgl6TG9DpXT+sOZL4dSeqLd/yS1BiDX5IaY/BLUmMMfklqj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gnj+MNfp7Jy2vJvB7/tMU5erMd7xa0moqm8x6AqZPoHclxjMhgiDu93Z+s6HsSfkH+7m4D9vrp279xf+CHhFVX2va/4aMJHkOd0+hyV5ejd98b8m2XOX/qpZzvmsJD/WLR8CPAP4Rrf5kGk1/Xd+8LM+DtjWdTNNP+/NwOu7c61I8vg5f/of+CpwcvfwGRi84azGGPxaSi4Fjpu2/ibgNd1MkeczeObpgnTh/EEGXT9/xWDmybm8GvhR4ONdn/xnusd5nge8K8ndDLpP9oyieQ1weZIvM3hfYSZPBD7VDfW8h8Eb2+/vtv0b8PQkGxl04+x5Y/n/Mngy2E388PTIFzHourqXwRu383ZbAXR/xH4N+FyS24GHgJm6ibSMOTuntATsGY0zomsdVVWPds8kuBx4oKreM4pra2nwjl9qz2u7N6fvB57AYJSPGuIdvyQ1xjt+SWqMwS9JjTH4JakxBr8kNcbgl6TG/Afn0ygiPQR+RgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Get the ordered eigenvalues\n",
    "ordered_eigvals = np.linalg.eigh(corr_matrix)[0]\n",
    "spacings = np.diff(ordered_eigvals)\n",
    "\n",
    "avg_spacing = np.zeros_like(spacings)\n",
    "for i in range(2, len(ordered_eigvals)-3):\n",
    "    avg_spacing[i] = (spacings[i-2] + spacings[i-1] + spacings[i] + spacings[i+1] + spacings[i+2]) / 5\n",
    "\n",
    "avg_spacing[0] = avg_spacing[2]\n",
    "avg_spacing[1] = avg_spacing[2]\n",
    "avg_spacing[len(ordered_eigvals)-2] = avg_spacing[len(ordered_eigvals)-4]\n",
    "avg_spacing[len(ordered_eigvals)-3] = avg_spacing[len(ordered_eigvals)-4]\n",
    "\n",
    "\n",
    "fig, ax = plt.subplots(1,1)\n",
    "ax.hist(spacings/avg_spacing, bins=20)\n",
    "ax.set_xlabel(r'Normalized Spacing')\n",
    "ax.set_ylabel('Counts')\n",
    "fig.savefig('../output/unfolded_eigval_dist.pdf')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
